package com.neusoft.nepa.controller;

import com.neusoft.nepa.UserLogin;
import com.neusoft.nepa.entity.ProvinceProblem;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

public class ProvinceGroupInspectionController {
    public void changeToPublicSupervise(ActionEvent event) throws IOException {
        Stage stage = (Stage) ((javafx.scene.Node) event.getSource()).getScene().getWindow();
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/com/neusoft/nepa/view/PublicSUperviseList.fxml"));
        Parent parent = fxmlLoader.load();
        Scene newScene = new Scene(parent, 960, 540);
        stage.setTitle("NorthEast Software Corporation Public Supervisor Platform Manage System");
        stage.setScene(newScene);
        stage.show();
    }
    @FXML
    private void logOut(ActionEvent event) throws IOException {
        Stage stage = (Stage) ((javafx.scene.Node) event.getSource()).getScene().getWindow();
        stage.getProperties().remove("userName");
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/com/neusoft/nepa/view/AdminLoginView.fxml"));
        Parent fxmlContent = fxmlLoader.load();
        BackgroundPane backgroundPane = new BackgroundPane();
        backgroundPane.getChildren().add(fxmlContent);
        fxmlContent.setStyle("-fx-background-color: transparent;");
        Scene newScene = new Scene(backgroundPane, 960, 540);
        stage.setTitle("NorthEast Software Corporation Public Supervisor Platform Manage System");
        stage.setScene(newScene);
        stage.show();
    }
    public void changeToAQI(ActionEvent event) throws IOException {
        Stage stage = (Stage) ((javafx.scene.Node) event.getSource()).getScene().getWindow();
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/com/neusoft/nepa/view/AQIConfigList.fxml"));
        Parent parent = fxmlLoader.load();
        Scene newScene = new Scene(parent, 960, 540);
        stage.setTitle("NorthEast Software Corporation Public Supervisor Platform Manage System");
        stage.setScene(newScene);
        stage.show();
    }
    @FXML
    private TableView<ProvinceProblem> adminDataTableView;
    @FXML
    private TableColumn<ProvinceProblem, String> province;
    @FXML
    private TableColumn<ProvinceProblem, Number> so2;
    @FXML
    private TableColumn<ProvinceProblem, Number> co;
    @FXML
    private TableColumn<ProvinceProblem, Number> pm25;
    @FXML
    private TableColumn<ProvinceProblem, Number> aqi;

    @FXML
    private ObservableList<ProvinceProblem> dataList = FXCollections.observableArrayList();
    public void initialize() {

        System.out.println("执行了初始化方法");
        initColumn();
        // 重要：设置表格数据源（必须在添加数据前完成）
        adminDataTableView.setItems(dataList);
        Platform.runLater(() -> {
            getInfoMap();
        });
    }

    private void initColumn() {

        province.setPrefWidth(120); // 设置列宽
        // 绑定 ProvinceProblem 的 provinceProperty()
        province.setCellValueFactory(cellData -> cellData.getValue().provinceProperty());

        // 2. SO2 列（整数类型）

        so2.setPrefWidth(80);
        // 绑定 so2Property()（IntegerProperty 继承自 NumberProperty）
        so2.setCellValueFactory(cellData -> cellData.getValue().so2Property());

        // 3. CO 列

        co.setPrefWidth(80);
        co.setCellValueFactory(cellData -> cellData.getValue().coProperty());

        // 4. PM2.5 列

        pm25.setPrefWidth(80);
        pm25.setCellValueFactory(cellData -> cellData.getValue().pm25Property());

        // 5. AQI 列
        aqi.setPrefWidth(80);
        aqi.setCellValueFactory(cellData -> cellData.getValue().aqiProperty());

        // 将所有列添加到 TableView
    }
    private void getInfoMap()
    {
        System.out.println("get information map");
        UserLogin a = new UserLogin();
        ArrayList<ProvinceProblem> provinceData = new ArrayList<>();
        try {
             provinceData = a.ProvinceListDetect();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        for(ProvinceProblem pr : provinceData)
        {
            dataList.add(new ProvinceProblem(pr.getProvince(),pr.getSo2(),pr.getCo(),pr.getPm25(),pr.getAqi()));
        }
    }
}
